package com.peas.platform.module.icims.expenditure.basal.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.basal.adapter.repository.ExpenditureAccountRepository;
import com.peas.platform.module.icims.expenditure.basal.adapter.repository.po.ExpenditureAccountPo;
import com.peas.platform.module.icims.expenditure.basal.domain.ExpenditureAccountDomain;
import com.peas.platform.module.icims.expenditure.basal.dto.ExpenditureAccountDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ExpenditureAccountDomainImpl extends BaseDomainImpl<ExpenditureAccountDto, ExpenditureAccountPo> implements ExpenditureAccountDomain {

    @Resource
    private ExpenditureAccountRepository expenditureAccountRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ExpenditureAccountPo> getMapper() {
        return expenditureAccountRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureAccountPo>> getRepositoryClass() {
        return ExpenditureAccountRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureAccountDto dto) {
        if (StrUtil.isBlank(dto.getAccountCode())) {
            String code = sequenceService.genSequence("expenditure", "ExpenditureAccountDto", "EAD", 4, 1);
            dto.setAccountCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("accountCode", dto.getAccountCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureAccountDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("accountCode", dto.getAccountCode());
        return result;
    }

}
